﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HRM.BLL;
using HRM.DAL.DAO;

namespace HRM.UI.HRAdminUI.EmpModule
{
    public partial class AddCertificates : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                if (Session["empCode"] != null)
                {
                    employeeCode.InnerText = Session["empCode"].ToString();
                    textboxCode.Visible = false;
                }
                else
                {
                    Label1.Visible = false;
                    employeeCode.Visible = false;
                    employeeCode.InnerText = "";
                }

            }

        }

        protected void SaveCertificates(object sender, EventArgs e)
        {
            try
            {
                CertificateManager certificateManager = new CertificateManager();
                List<Certificate> certificates = new List<Certificate>();
                Employee employee = new Employee();
                Certificate aCertificate = new Certificate();

                Code code = new Code();

                if (employeeCode.InnerText != "")
                {
                    code.CodeName = employeeCode.InnerText;
                }
                else
                {
                    code.CodeName = txtEmpCode.Value;
                }

                employee.EmpCode = code;
                aCertificate.Employee = employee;
                aCertificate.Exam = txtExamName.Value;
                string sscCertificate = Path.GetFileName(UploadSSC.PostedFile.FileName);
                UploadSSC.PostedFile.SaveAs(Server.MapPath("~/EmpCertificates/") + code.CodeName + "-" + txtExamName.Value + "-" + sscCertificate);
                aCertificate.FileName = "~/EmpCertificates/" + code.CodeName + "-" + txtExamName.Value + "-" + sscCertificate;
                if (certificateManager.SaveCertificate(aCertificate))
                {
                    MsgLabel.Text = "Certificate Saved Successfully";
                    MsgLabel.ForeColor = Color.Green;

                }
                else
                {
                    MsgLabel.Text = "Sorry Certificate Not Saved";
                    MsgLabel.ForeColor = Color.Red;
                }
            }
            catch (Exception exception)
            {
                MsgLabel.Text = exception.Message;
                MsgLabel.ForeColor = Color.Red;
            }
        }
    }
}